package com.smart.manager.task;


import com.smart.common.annotation.TaskBean;
import com.smart.common.annotation.TaskMethod;
import com.smart.common.entities.domain.TaskResult;
import com.smart.manager.member.entities.models.MemberMaterialPig;
import com.smart.manager.member.entities.models.query.QMemberMaterialPig;
import com.smart.manager.product.entities.enums.PigStatus;
import com.smart.manager.product.entities.views.PigLevelView;
import com.smart.manager.product.entities.views.query.QPigLevelView;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author leo
 * @date 2023/11/01
 */
@Log4j2
@Component
@TaskBean
@RequiredArgsConstructor
public class DayTask {

    @TaskMethod(title = "猪仔自动增长")
    public synchronized TaskResult cleanRemoveRecord() {
        log.warn("猪仔自动增长");
        List<MemberMaterialPig> pigs = new QMemberMaterialPig().status.eq(PigStatus.NORMAL).findList();
        for (MemberMaterialPig pig : pigs) {
            PigLevelView pigLevel = pig.getPigLevel();
            new QPigLevelView()
                    .day.gt(pigLevel.getDay())
                    .kind.eq(pigLevel.getKind())
                    .breed.eq(pigLevel.getBreed())
                    .order().day.asc()
                    .setMaxRows(1)
                    .findOneOrEmpty().ifPresent(x -> {
                        pig.setPigLevel(x).update();
                    });
        }
        return new TaskResult(true, "增长成功!");
    }
}
